package com.dogkr.server.web;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.dogkr.common.ServerAction;
import com.dogkr.server.entity.Host;
import com.dogkr.server.entity.Order;
import com.dogkr.server.entity.Order.OrderStatus;
import com.dogkr.server.entity.OrderServer;
import com.dogkr.server.entity.PetServer;
import com.dogkr.server.entity.User;
import com.dogkr.server.service.HostService;
import com.dogkr.server.service.ManagerService;
import com.dogkr.server.service.MessageService;
import com.dogkr.server.service.OrderService;
import com.dogkr.server.service.PetServerService;
import com.dogkr.server.service.UserService;
import com.sean.common.page.Pagination;
import com.sean.common.page.ResultPagination;
import com.sean.common.utils.DateUtils;


// 设置Struts2注解
@ParentPackage(value = "login-default")
@Namespace(value = "/order")
@ResultPath("/WEB-INF/jsps")
// 页面文件空间
@Results({
	@Result(name = "Show", location = "show.jsp"),
	
	@Result(name = "Quotation", location = "win_quotation.jsp"),
	
	@Result(name = "Appraise", location = "win_appraise.jsp")
})
public class OrderAction extends ServerAction {
	private static final long serialVersionUID = 1L;
	
	private Order order;
	
	private List<OrderServer> serverList;
	
	private BigDecimal discount;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private PetServerService petServerService;
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private HostService hostService;
	
	@Autowired
	private UserService userService;
	
	@Action(value = "show")
	public String orderShow() {
		Order order = orderService.selectByPrimaryKeyWithAll(id);
		if(order.isMyOrder(getLoginUser())){
			messageService.updateReadByOrderKey(id);
			put("order", order);
			if(order.getStatus() == OrderStatus.AGREE.getVal() || order.getStatus() == OrderStatus.ASSESS.getVal()) {
				Host host = hostService.selectByPrimaryKey(order.getHostId());
				put("host", host);
				User user = userService.selectByPrimaryKey(host.getUserId());
				put("hostUser", user);
			}
		}
		return "Show";
	}
	
	@Action(value = "create")
	public void create() {
		if(order != null && CollectionUtils.isNotEmpty(order.getPetList())) {
			User user = this.getLoginUser();
			order.setUserId(user.getId());
			order.setUserName(user.getUserNickName());
			orderService.createOrder(order);
		} else {
			res = this.getErrRes();
		}
		outJson(res);
	}
	
	@Action(value="get")
	public void get() {
		Order order = orderService.selectByPrimaryKeyWithAll(id);
		outJson(order);
	}
	/**
	 * 谢绝预约.
	 */
	@Action(value="reject") 
	public void reject() {
		orderService.rejectOrder(id, this.getLoginUser().getHost().getId());
		outJson(res);
	}
	
	/**
	 * 接受预约.
	 */
	@Action(value="agree") 
	public void agree() {
		orderService.agreeOrder(id, this.getLoginUser().getHost().getId());
		outJson(res);
	}
	
	/**
	 * 报价单.
	 */
	@Action(value="quotation") 
	public String quotation() {
		User user = this.getLoginUser();
		Order order = orderService.selectByPrimaryKeyWithAll(id);
		if(order.isMyOrder(user)) {
			put("order", order);
			int days = 0;
			try {
				days = DateUtils.getDays(order.getOrderStartDate(), order.getOrderEndDate());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			put("days", days);
		}
		List<PetServer> serverList = petServerService.selectHostPetServerWithAll(user.getHost().getId());
		put("serverList", serverList);
		return "Quotation";
	}
	
	/**
	 * 评价.
	 */
	@Action(value="appraise") 
	public String appraise() {
		User user = this.getLoginUser();
		Order order = orderService.selectByPrimaryKeyWithAll(id);
		if(order.isMyOrder(user)) {
			put("order", order);
			int days = 0;
			try {
				days = DateUtils.getDays(order.getOrderStartDate(), order.getOrderEndDate());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			put("days", days);
			return "Appraise";
		}
		return null;
	}
	
	//预约列表接口
	@Action(value = "list_order")
	public void listOrder() {
		Pagination pagination = this.getPagination();
		//收到的
		if("received".equals(this.get("orderType"))) {
			pagination.putParam("hostId", this.getLoginUser().getHost().getId());
			//未处理的
			if(this.get("untreated") != null) {
				pagination.putParam("status", Order.OrderStatus.CREATE.getVal());
			}
		} else {	//发送的
			pagination.putParam("userId", this.getLoginUser().getId());
		}
		ResultPagination<Order> result = orderService.selectList(pagination);
		outJson(result);
	}
	
	/**
	 * 创建报价单
	 */
	@Action(value = "saveQuotation")
	public void createQuotation() {
		try {
			order = orderService.selectByPrimaryKey(order.getId());
			if(order.isMyOrder(this.getLoginUser()) && serverList!= null) {
				orderService.createOrderServer(order, discount, serverList);
			}
			outJson(res);
		} catch (ParseException e) {
			outJson(this.getErrRes());
		}
	}
	
	/**
	 * 保存评论
	 */
	@Action(value = "saveAppraise")
	public void saveAppraise() {
		Order o = orderService.selectByPrimaryKey(order.getId());
		if(o.isMyOrder(this.getLoginUser()) && o.getStatus() == OrderStatus.AGREE.getVal()) {
			
			orderService.saveAppraise(order);
			hostService.updateAppraise(o.getHostId());
		}
		outJson(res);
	}
	
	//订单列表
	@Action(value = "list_indent")
	public void listIndent() {
		Pagination pagination = this.getPagination();
		//收到的
		if("received".equals(this.get("orderType"))) {
			pagination.putParam("hostId", this.getLoginUser().getHost().getId());
		} else {
			pagination.putParam("userId", this.getLoginUser().getId());
		}
		pagination.putParam("orderType", Order.OrderStatus.QUOTATION.getVal());
		ResultPagination<Order> result = orderService.selectList(pagination);
		outJson(result);
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public List<OrderServer> getServerList() {
		return serverList;
	}

	public void setServerList(List<OrderServer> serverList) {
		this.serverList = serverList;
	}

	public BigDecimal getDiscount() {
		return discount;
	}

	public void setDiscount(BigDecimal discount) {
		this.discount = discount;
	}
}
